package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.upmp.domain.UpmpUserRole;
import cc.rengu.redp.bizimpl.upmp.dao.UpmpUserRoleMapper;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpUserRoleService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author System
 */
@Service("upmpUserRoleService")
@DS("upmp")
public class UpmpUserRoleServiceImpl extends ServiceImpl<UpmpUserRoleMapper, UpmpUserRole> implements IUpmpUserRoleService {

    @Override
    @Transactional
    public void deleteUserRolesByRoleId(String[] roleIds) {
        Arrays.stream(roleIds).forEach(id -> baseMapper.deleteByRoleId(id));
    }

    @Override
    @Transactional
    public void deleteUserRolesByUserId(String[] userIds) {
        Arrays.stream(userIds).forEach(id -> baseMapper.deleteByUserId(id));
    }

    @Override
    public List<String> findUserIdsByRoleId(String[] roleIds) {

        List<UpmpUserRole> list = baseMapper.selectList(new LambdaQueryWrapper<UpmpUserRole>().in(UpmpUserRole::getRoleId, String.join(",", roleIds)));
        return list.stream().map(userRole -> String.valueOf(userRole.getUserId())).collect(Collectors.toList());
    }

}
